﻿/**
 *    Copyright 2012 Yohan Courbe
 *
 *  Licensed under the Apache License, Version 2.0 (the "License") 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


using System;
using System.Drawing;
using CountryLookup;

namespace NetworkAnalyzer.Engines.Graphics.Radar
{
    public class RadarCountry
    {
        public Country Country;
        public float Activity;

        /// <summary>
        /// 
        /// </summary>
        public float PointRadius
        {
            get
            {
                float radius = 0F;

                switch (GraphicsRadarSystem.Mode)
                {
                    case GraphicsRadarMode.TotalBandwidthRadarMode:
                        radius = Activity * 0.01f;

                        break;
                }

                if (radius > 25) radius = 25;

                return radius;
            }
        }

        public float PointColor
        {
            get
            {
                if (Activity > 1000000000)
                {
                    return 1.0f;
                }

                if (Activity > 1000000)
                {
                    return 0.6f;
                }

                if (Activity > 1000)
                {
                    return 0.4f;
                }

                return 0.2f;
            }
        }

        public float Angle
        {
            get
            {
                double angle, radius;
                Tools.CartesianToPolar(Country.Longitude, Country.Latitude, out angle, out radius);
                var result = (float)((float)angle / Math.PI * 180);

                if (Country.Latitude > 0 && Country.Longitude > 0) // Belgium etc
                {
                    return result + 180;
                }

                if (Country.Latitude > 0 && Country.Longitude < 0) // America
                {
                    return result + 180;
                }

                if (Country.Latitude < 0 && Country.Longitude < 0) // Argentina
                {
                    return result;
                }

                if (Country.Latitude < 0 && Country.Longitude > 0) // South africa
                {
                    return result + 360;
                }

                return result;
            }
        }

        public bool Shown
        {
            get { return _shown; }
            set
            {
                _ttl--;
                _shown = value;
            }
        }

        private int _ttl = 24 * 4;
        private bool _shown;
        private float _tempsActivity;
        public int Ttl {get { return _ttl; }}


        public RadarCountry(Country country)
        {
            Country = country;
        }

        public void IncreaseActivity(int length)
        {
            Activity += length;

            if (_shown)
            {
                _tempsActivity += length;
            }

            //Console.WriteLine("Inc {0} {1}", Country.CountryCode, Activity);
        }

        public void ResetActivity()
        {
            Activity = 0;
            Activity = _tempsActivity;
            Shown = false;
        }

        public void DecreaseActivity()
        {
            if (Activity > 1)
            {
                Activity /= 1000;
                //Console.WriteLine("Dec {0} {1}", Country.CountryCode, Activity);
            }
        }

        public override bool Equals(object obj)
        {
            var country = (Country)obj;

            return Country.CountryCode.Equals(country.CountryCode);
        }
    }
}
